package NeighborNodeSelection;

import giny.view.NodeView;
import giny.model.Node;

import java.awt.event.ActionEvent;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.swing.JOptionPane;

import java.util.Iterator;

import cytoscape.CyNetwork;
import cytoscape.CyNode;
import cytoscape.Cytoscape;
import cytoscape.data.Semantics;
import cytoscape.plugin.CytoscapePlugin;
import cytoscape.util.CytoscapeAction;
import cytoscape.view.CyNetworkView;

public class NeighborNodeSelection extends CytoscapePlugin{

	/* This constructor creates an action 	and adds it to the plugins Menu */
	public NeighborNodeSelection(){
//		Create a new action to respond to menu activation
		NeighborNodeSelectionAction action = new NeighborNodeSelectionAction();
//		Set the preferred menu
		action.setPreferredMenu("Plugins");
//		Add it to the menu
		Cytoscape.getDesktop().getCyMenus().addAction(action);
		
	}
	
	 /**
	  * Gives a description of this plugin.
	  */
	 public String describe() {
	          StringBuffer sb = new StringBuffer();
	          sb.append("For every currently selected node in the graph view, this ");
	          sb.append("plugin additionally selects each neighbor of that node if ");
	          sb.append("the canonical names of the two nodes have the same last letter.");
	          return sb.toString();
	}
	
	/**
	 * This class gets attached to the menu item
	 */
	public class NeighborNodeSelectionAction extends CytoscapeAction{
//	This constructor sets the text to be seen in the menu item 
		public NeighborNodeSelectionAction() {
			super("NeighborNodeSelectionAction");
			
		}
		
//		This method is called when the user select the plugin on the menu item
		public void actionPerformed(ActionEvent ae) {
			// TODO Auto-generated method stub
			
//			Get the current network object, this contains the graph
			CyNetwork network = Cytoscape.getCurrentNetwork();
			
//			Get the current network view object 
			CyNetworkView view = Cytoscape.getCurrentNetworkView();
			
//			if either of network or view is null don't continue"
			if (network == null || view == null) {
				return;
			}
			
			
//			put a dialog box if no node is selected
			if (view.getSelectedNodes().size() == 0)
				JOptionPane.showMessageDialog(view.getComponent(), "Please select one or more nodes");
			
//			A container to hold the selected objects
			Set selectedNodesViews = new HashSet();
			
//			iterate over every node view
			for (java.util.Iterator itr= view.getSelectedNodes().iterator(); itr.hasNext();){
				NodeView nodeView = (NodeView)itr.next();
//				get the corresponding node in the network
				CyNode node = (CyNode) nodeView.getNode();
//				get the neighbors of that node
				List neighbors = network.neighborsList(node);
//				iterate over the neighbors
				for(Iterator itr2=(Iterator) neighbors.iterator();itr2.hasNext(); ){
					CyNode neighbor =(CyNode) ((java.util.Iterator) itr2).next();
//					get the view on this neighbor
					NodeView neighborView = view.getNodeView(neighbor);
//					add this neighborView to the container
					selectedNodesViews.add(neighborView);
				}
			}
			
//			Now go through each view in the container
			for(Iterator itr=(Iterator) selectedNodesViews.iterator(); itr.hasNext();){
				NodeView viewToBeSelected = (NodeView) ((java.util.Iterator) itr).next();
				viewToBeSelected.setSelected(true);
			}						
		}		
	}
	
	/**
     * Gets the canonical name of the given node from the network object
     * and returns a String holding just the last letter of that name.
     *
     * Returns null if a valid name cannot be obtained.
     */
    private String getLastLetter(CyNetwork network, CyNode node) {
   
        String canonicalName = (String) network.getNodeAttributeValue(node, Semantics.CANONICAL_NAME);
        //return nothing if we can't get a valid name
        if (canonicalName == null || canonicalName.length() == 0) {return null;}
        //extract the last letter
        int length = canonicalName.length();
        String lastLetter = canonicalName.substring(length-1);
        return lastLetter;
    }
    
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
